home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.event.ChangeListener;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Toolkit;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
-
- public class StyleContext implements Serializable, AbstractDocument.AttributeContext {
- private static StyleContext defaultContext;
- public static final String DEFAULT_STYLE = "default";
- private static Hashtable freezeKeyMap;
- private static Hashtable thawKeyMap;
- private Style styles = new NamedStyle(this, (Style)null);
- private transient FontKey fontSearch = new FontKey((String)null, 0, 0);
- private transient Hashtable fontTable = new Hashtable();
- private transient Hashtable attributesPool = new Hashtable();
- private transient MutableAttributeSet search = new SimpleAttributeSet();
- private int unusedSets;
- static final int THRESHOLD = 9;
- static Class class$com$sun$java$swing$event$ChangeListener;
-
- public StyleContext() {
- this.addStyle("default", (Style)null);
- }
-
- public synchronized AttributeSet addAttribute(AttributeSet old, Object name, Object value) {
- if (old.getAttributeCount() + 1 <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(old);
- this.search.addAttribute(name, value);
- this.reclaim(old);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet ma = this.getMutableAttributeSet(old);
- ma.addAttribute(name, value);
- return ma;
- }
- }
-
- public synchronized AttributeSet addAttributes(AttributeSet old, AttributeSet attr) {
- if (old.getAttributeCount() + attr.getAttributeCount() <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(old);
- this.search.addAttributes(attr);
- this.reclaim(old);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet ma = this.getMutableAttributeSet(old);
- ma.addAttributes(attr);
- return ma;
- }
- }
-
- public void addChangeListener(ChangeListener l) {
- this.styles.addChangeListener(l);
- }
-
- public Style addStyle(String nm, Style parent) {
- Style style = new NamedStyle(this, nm, parent);
- if (nm != null) {
- this.styles.addAttribute(nm, style);
- }
-
- return style;
- }
-
- protected int getCompressionThreshold() {
- return 9;
- }
-
- public static final StyleContext getDefaultStyleContext() {
- if (defaultContext == null) {
- defaultContext = new StyleContext();
- }
-
- return defaultContext;
- }
-
- public AttributeSet getEmptySet() {
- return SimpleAttributeSet.EMPTY;
- }
-
- public Font getFont(AttributeSet attr) {
- int style = 0;
- if (StyleConstants.isBold(attr)) {
- style |= 1;
- }
-
- if (StyleConstants.isItalic(attr)) {
- style |= 2;
- }
-
- String family = StyleConstants.getFontFamily(attr);
- int size = StyleConstants.getFontSize(attr);
- return this.getFont(family, style, size);
- }
-
- public Font getFont(String family, int style, int size) {
- this.fontSearch.setValue(family, style, size);
- Font f = (Font)this.fontTable.get(this.fontSearch);
- if (f == null) {
- f = new Font(family, style, size);
- FontKey key = new FontKey(family, style, size);
- this.fontTable.put(key, f);
- }
-
- return f;
- }
-
- public FontMetrics getFontMetrics(Font f) {
- return Toolkit.getDefaultToolkit().getFontMetrics(f);
- }
-
- AttributeSet getImmutableUniqueSet() {
- SmallAttributeSet key = new SmallAttributeSet(this, this.search);
- SmallAttributeSet a = (SmallAttributeSet)this.attributesPool.get(key);
- if (a == null) {
- a = key;
- this.attributesPool.put(key, key);
- }
-
- ++a.nrefs;
- return a;
- }
-
- MutableAttributeSet getMutableAttributeSet(AttributeSet a) {
- return (MutableAttributeSet)(a instanceof MutableAttributeSet ? (MutableAttributeSet)a : new SimpleAttributeSet(a));
- }
-
- public Style getStyle(String nm) {
- return (Style)this.styles.getAttribute(nm);
- }
-
- public Enumeration getStyleNames() {
- return this.styles.getAttributeNames();
- }
-
- public static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException {
- int n = in.readInt();
-
- for(int i = 0; i < n; ++i) {
- Object key = in.readObject();
- Object value = in.readObject();
- Object staticKey = thawKeyMap.get(key);
- if (staticKey != null) {
- key = staticKey;
- }
-
- a.addAttribute(key, value);
- }
-
- }
-
- private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
- this.fontSearch = new FontKey((String)null, 0, 0);
- this.fontTable = new Hashtable();
- this.search = new SimpleAttributeSet();
- this.attributesPool = new Hashtable();
- s.defaultReadObject();
- }
-
- public void reclaim(AttributeSet a) {
- if (a instanceof SmallAttributeSet) {
- SmallAttributeSet sa = (SmallAttributeSet)a;
- --sa.nrefs;
- if (sa.nrefs <= 0) {
- ++this.unusedSets;
- if (this.unusedSets > 10 && this.unusedSets > this.attributesPool.size() / 10) {
- if (SwingUtilities.isEventDispatchThread()) {
- this.removeUnusedSets();
- } else {
- Runnable callRemoveUnused = new 1(this);
- SwingUtilities.invokeLater(callRemoveUnused);
- }
- }
- }
- }
-
- }
-
- public static void registerStaticAttributeKey(Object key) {
- String ioFmt = key.getClass().getName() + "." + key.toString();
- if (freezeKeyMap == null) {
- freezeKeyMap = new Hashtable();
- thawKeyMap = new Hashtable();
- }
-
- freezeKeyMap.put(key, ioFmt);
- thawKeyMap.put(ioFmt, key);
- }
-
- public synchronized AttributeSet removeAttribute(AttributeSet old, Object name) {
- if (old.getAttributeCount() - 1 <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(old);
- this.search.removeAttribute(name);
- this.reclaim(old);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet ma = this.getMutableAttributeSet(old);
- ma.removeAttribute(name);
- return ma;
- }
- }
-
- public synchronized AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) {
- if (old.getAttributeCount() <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(old);
- this.search.removeAttributes(attrs);
- this.reclaim(old);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet ma = this.getMutableAttributeSet(old);
- ma.removeAttributes(attrs);
- return ma;
- }
- }
-
- public synchronized AttributeSet removeAttributes(AttributeSet old, Enumeration names) {
- if (old.getAttributeCount() <= this.getCompressionThreshold()) {
- this.search.removeAttributes(this.search);
- this.search.addAttributes(old);
- this.search.removeAttributes(names);
- this.reclaim(old);
- return this.getImmutableUniqueSet();
- } else {
- MutableAttributeSet ma = this.getMutableAttributeSet(old);
- ma.removeAttributes(names);
- return ma;
- }
- }
-
- public void removeChangeListener(ChangeListener l) {
- this.styles.removeChangeListener(l);
- }
-
- public void removeStyle(String nm) {
- this.styles.removeAttribute(nm);
- }
-
- synchronized void removeUnusedSets() {
- Vector rmList = new Vector();
- Enumeration sets = this.attributesPool.keys();
-
- while(sets.hasMoreElements()) {
- SmallAttributeSet set = (SmallAttributeSet)sets.nextElement();
- if (set.nrefs <= 0) {
- rmList.addElement(set);
- }
- }
-
- sets = rmList.elements();
-
- while(sets.hasMoreElements()) {
- this.attributesPool.remove(sets.nextElement());
- }
-
- this.unusedSets = 0;
- }
-
- public String toString() {
- this.removeUnusedSets();
- String s = "";
-
- SmallAttributeSet set;
- for(Enumeration sets = this.attributesPool.keys(); sets.hasMoreElements(); s = s + set + "\n") {
- set = (SmallAttributeSet)sets.nextElement();
- }
-
- return s;
- }
-
- public static void writeAttributeSet(ObjectOutputStream out, AttributeSet a) throws IOException {
- int n = a.getAttributeCount();
- out.writeInt(n);
- Enumeration keys = a.getAttributeNames();
-
- while(keys.hasMoreElements()) {
- Object key = keys.nextElement();
- if (key instanceof Serializable) {
- out.writeObject(key);
- } else {
- Object ioFmt = freezeKeyMap.get(key);
- out.writeObject(ioFmt);
- }
-
- Object value = a.getAttribute(key);
- out.writeObject(value);
- }
-
- }
-
- private void writeObject(ObjectOutputStream s) throws IOException {
- this.removeUnusedSets();
- s.defaultWriteObject();
- }
- }
-